angular.module('myApp', ['infinite-scroll']);
showLoading();

function circleGoods($scope, $http) {
	var uid = readData(localStorage.account);
	var gid = readData(localStorage.guideId);
	//轮播图广告图片
	urlBanner = getUrl("75getAdvert");
	paramsBanner = {
		"advertLevel": "shop",
		"userId": gid
	};
	$http({
		method: 'POST',
		url: urlBanner,
		timeout: 10000,
		//cache: true,
		params: paramsBanner
	}).success(function(data, status, headers, config) {
		if (data.lunList) {
			var str = data.lunList.tops[0].images;
			$scope.banners = str.split(',');
			setTimeout(function() {
				var mySwiper = new Swiper('.swiper-container', {
					direction: 'horizontal',
					loop: true,
					autoplay: 5000,
					speed: 1500,
					pagination: '.swiper-pagination'
				});
			}, 1000);
		}
	}).error(function() {
		console.log("circleShop lunbotu is error!");
	});
	//我的关注
	urlMine = getUrl("myAttention");
	headerMine = getHeader();
	paramsMine = 'indexpage=0&pageSize=5&accountId=' + uid + '&userId=' + gid;
	$http({
		method: 'POST',
		url: urlMine,
		headers: headerMine,
		timeout: 10000,
		data: paramsMine
	}).success(function(data, status, headers, config) {
		if (data.obj) {
			$scope.mine = data.obj.list;
			if (data.obj.list.length <= 0) {
				$scope.ishide = false;
			} else {
				$scope.ishide = true;
				for (var i = 0; i < $scope.mine.length; i++) {
					if (!$scope.mine[i].picture) {
						$scope.mine[i].picture = '../../image/small.png';
					}
					if ($scope.mine[i].pricename) {
						$scope.mine[i].hide = true;
					} else {
						$scope.mine[i].hide = false;
					}
					if ($scope.mine[i].ruleflag == 1) { //无促销标志
						$scope.mine[i].cu = true;
					} else {
						$scope.mine[i].cu = false;
					}
				}
			}
		} else {
			$scope.mine = null;
		};
		removeLoading()
	}).error(function() {
		console.log("circleShop wodegunzhu is error!");
	});
	//获取大家关注
	$scope.pages = 0;
	//默认加载第一页，初始值0
	$scope.all = [];
	$scope.totalpage = 0;
	urlAll = getUrl("allAttention");
	headerAll = getHeader();
	paramsAll = 'indexpage=0&pageSize=5&id=' + gid;
	//大家关注
	$http({
		method: 'POST',
		url: urlAll,
		headers: headerAll,
		timeout: 10000,
		data: paramsAll
	}).success(function(data, status, headers, config) {
		if (data.obj) {
			//alert(JSON.stringify(data.obj));
			var alls = data.obj.list;
			if (data.obj.list.length <= 0) {
				$scope.isshow = false;
			} else {
				$scope.isshow = true;
			}
			for (var i = 0; i < alls.length; i++) {
				$scope.all.push(alls[i]);
			}
			for (var i = 0; i < $scope.all.length; i++) {
				if ($scope.all[i].pricename) {
					$scope.all[i].hide = true;
				} else {
					$scope.all[i].hide = false;
				}
				if ($scope.all[i].ruleflag == 1) {
					$scope.all[i].cu = true;
				} else {
					$scope.all[i].cu = false;;
				}
				if (!$scope.all[i].picture) {
					$scope.all[i].picture = '../../image/small.png';
				}
			}
			$scope.totalpage = data.obj.totalpage;
			//下拉页面+1
			$scope.pages = $scope.pages + 1;
		} else {
			$scope.all = []
		}
	}).error(function() {
		console.log("大家关注错误")
	});
	//下拉刷新
	$scope.loadMore = function() {
			if ($scope.pages < $scope.totalpage) {
				$scope.pages = $scope.pages + 1;
				$http({
					method: 'POST',
					url: urlAll,
					headers: headerAll,
					timeout: 10000,
					//cache:true,
					data: 'indexpage=' + $scope.pages + '&pageSize=5&id=' + gid
				}).success(function(data, status, headers, config) {
					if (data.obj) {
						var alls = data.obj.list;
						for (var i = 0; i < alls.length; i++) {
							$scope.all.push(alls[i])
						}
						for (var i = 0; i < $scope.all.length; i++) {
							$scope.all[i].cu = "block";
							if ($scope.all[i].pricename) {
								$scope.all[i].hide = true;
							} else {
								$scope.all[i].hide = false;
							}
							if ($scope.all[i].ruleflag == 1) {
								$scope.all[i].cu = true;
							} else {
								$scope.all[i].cu = false;
							}
							if (!$scope.all[i].picture) {
								$scope.all[i].picture = '../../image/small.png';
							}
						}
						$scope.totalpage = data.obj.totalpage;
						//下拉页面+1
					} else {
						$scope.all = [];
						$scope.pages = $scope.pages - 1;
					}
				}).error(function() {
					$scope.pages = $scope.pages - 1;
					console.log("大家关注错误")
				})
			}
		}
		//签到
	var checkining = false;
	var urlcheckStatus = getUrl("77getSignStatus");
	var headerCheckStatus = getHeader();
	var checkStatusParams = "accountId=" + uid + "&userId=" + gid;
	$http({
		method: 'POST',
		url: urlcheckStatus,
		timeout: 10000,
		headers: headerCheckStatus,
		data: checkStatusParams
	}).success(function(data, status, headers, config) {
		if (data.code == 0) {
			if (data.status == 1) {
				document.getElementById("checkin").src = "../../image/shopping/checkin2.png";
				checkining = true;
			}
		}
	}).error(function() {
		console.log("circleshop check is erro")
	})
	$scope.checkin = function() {
		var success = document.getElementById("success");
		var checkin = document.getElementById("checkin");
		var urlcheck = getUrl("76checkin");
		var headerCheck = getHeader();
		var checkParams = "accountId=" + uid + "&scoreCatalogCode=1&userId=" + gid;
		if (!checkining) {
			checkining = true;
			$http({
				method: 'POST',
				url: urlcheck,
				timeout: 10000,
				headers: headerCheck,
				data: checkParams
			}).success(function(data, status, headers, config) {
				if (data.code == 0) {
					checkin.src = "../../image/shopping/checkin2.png";
					success.className = "checkInSuccess animated zoomIn";
					setTimeout(function() {
						success.className = "checkInSuccess animated zoomOut";
					}, 2000)
				}
			}).error(function() {
				console.log("circleshop chinck is erro")
			})
		} else {
			mui.alert("今天已签到过，明天再来！")
		}
	}
	$scope.detail = function(id) {
			//获取商品ID，打开商品详情页
			localStorage.goodsId = writeData(id);
			OpenNewWin({
				id: "productDetail.html",
				url: "../../html/shopping/productDetail.html"
			})
		}
		//购物车商品数量
	$scope.buycarNum = function() {
		var buycarnum=document.getElementById("buyNum");
		if (localStorage.buyCar && readDataBuyCar(localStorage.buyCar) != "") {
			var buyCar = eval('(' + readDataBuyCar(localStorage.buyCar) + ')');
			var buyCarGuide = buyCar[readData(localStorage.guideId)];
			var num = 0
				//遍历购物车，查看商品是否存在（同型号同颜色）
			for (x in buyCarGuide) {
				num = num + parseInt(buyCarGuide[x]['number'])
			}
			if (num > 0) {
				buycarnum.innerText = num;
				buycarnum.style.display = "block";
			}else{
				buycarnum.style.display = "none";
			}
		}
	}
	$scope.buycarNum();
	//监听事件刷新购物车
	window.addEventListener("refreshBuyCar", function() {
		$scope.buycarNum();
	}, false)
}

function openShop() {
	OpenNewWin({
		id: "dianpu.html",
		url: "../../html/shopping/dianpu.html"
	})
}

function openbuyCar() {
	OpenNewWin({
		id: "buyCar.html",
		url: "../../html/shopping/buyCar.html"
	})
}